package cn.eli.vue.controller;

import cn.eli.vue.api.CommonResult;
import cn.eli.vue.entity.Artical;
import cn.eli.vue.entity.CommentArtical;
import cn.eli.vue.entity.LikeArtical;
import cn.eli.vue.entity.SubscribeArtical;
import cn.eli.vue.service.artical.ArticalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("文章模块")
@ResponseBody
@RestController
public class ArticalBussinessController {
    @Autowired
    ArticalService articalService;

    /**
     * 发布文章接口
     * 收取数据包：新发布的回答数据
     * 传回数据包：影响sql行数
     */
    @ApiOperation("发布文章")
    @RequestMapping(value = "/artical/articalpost",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalPost(@RequestBody Artical artical){
        int user_id = artical.getUser_id();
        String artical_title = artical.getArtical_title();
        String artical_discribe = artical.getArtical_discribe();
        String artical_img = artical.getArtical_img();

        if(artical_title==null||artical_title.equals("")){
            return CommonResult.validateFailed("文章的标题不可为空");
        }
        if(artical_discribe==null||artical_discribe.equals("")){
            return CommonResult.validateFailed("文章的正文内容不可为空");
        }
        if(artical_img==null||artical_img.equals("")){
            artical_img="";
        }

        int articalPost_res = articalService.ArticalPost(user_id,artical_title,artical_discribe,artical_img);

        if(articalPost_res!=0){
            return CommonResult.success(articalPost_res,"发布成功");
        }
        else{
            return CommonResult.validateFailed("发布失败");
        }
    }

    /**
     * 文章修改接口
     * 收取数据包：文章修改内容
     * 传回数据包：影响sql行数
     */
    @ApiOperation("修改文章")
    @RequestMapping(value = "/artical/articalmodify",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalModify(@RequestBody Artical artical){
        int artical_id = artical.getArtical_id();
        String artical_title = artical.getArtical_title();
        String artical_discribe = artical.getArtical_discribe();
        String artical_img = artical.getArtical_img();

        if(artical_title==null||artical_title.equals("")){
            return CommonResult.validateFailed("文章的标题不可为空");
        }
        if(artical_discribe==null||artical_discribe.equals("")){
            return CommonResult.validateFailed("文章的正文内容不可为空");
        }
        if(artical_img==null||artical_img.equals("")){
            artical_img="";
        }

        int articalModify_res = articalService.ArticalModify(artical_id,artical_title,artical_discribe,artical_img);

        if(articalModify_res!=0) {
            return CommonResult.success(articalModify_res, "修改成功");
        }
        else{
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 文章评论
     * 收取数据包：文章评论的数据
     * 传回数据包：影响sql行数
     */
    @ApiOperation("评论文章")
    @RequestMapping(value = "/artical/articalcomment",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalComment(@RequestBody CommentArtical commentArtical){
        int artical_id = commentArtical.getArtical_id();
        int user_id = commentArtical.getUser_id();
        String commentArtical_discribe = commentArtical.getCommentArtical_discribe();

        if(commentArtical_discribe == null){
            return CommonResult.failed("评论不可为空");
        }

        int articalComment_res = articalService.ArticalComment(artical_id,user_id,commentArtical_discribe);

        if(articalComment_res!=0){
            return CommonResult.success(articalComment_res,"文章评论成功");
        }
        else{
            return CommonResult.failed("评论发布失败");
        }
    }

    /**
     * 收藏文章
     * 收取数据包：用户id与文章id
     * 传回数据包：影响sql行数
     */
    @ApiOperation("收藏文章")
    @RequestMapping(value = "/artical/articalsubscribe",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalSubscribe(@RequestBody SubscribeArtical subscribeArtical){
        int artical_id = subscribeArtical.getArtical_id();
        int user_id = subscribeArtical.getUser_id();

        int articalSubscribeInsert_res = articalService.ArticalSubscribeInsert(artical_id,user_id);
        int articalSubscribeUpdate_res = articalService.ArticalSubscribeUpdate(artical_id,user_id);

        if(articalSubscribeInsert_res!=0&&articalSubscribeUpdate_res!=0){
            return CommonResult.success("收藏成功");
        }
        else{
            return CommonResult.failed("收藏失败");
        }
    }

    /**
     * 取消收藏文章
     * 收取数据包：用户id与文章id
     * 传回数据包：影响sql行数
     */
    @ApiOperation("取消收藏文章")
    @RequestMapping(value = "/artical/articalsubscribecancel",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalSubscribeCancel(@RequestBody SubscribeArtical subscribeArtical){
        int artical_id = subscribeArtical.getArtical_id();
        int user_id = subscribeArtical.getUser_id();

        int articalSubscribeCancelDelete_res = articalService.ArticalSubscribeCancelDelete(artical_id,user_id);
        int articalSubscribeCancelUpdate_res = articalService.ArticalSubscribeCancelUpdate(artical_id,user_id);

        if(articalSubscribeCancelDelete_res!=0&&articalSubscribeCancelUpdate_res!=0){
            return CommonResult.success("取消收藏成功");
        }
        else{
            return CommonResult.failed("取消收藏失败");
        }
    }

    /**
     * 点赞文章
     * 收取数据包：用户id与文章id
     * 传回数据包：影响sql行数
     */
    @ApiOperation("点赞文章")
    @RequestMapping(value = "/artical/articallike",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalLike(@RequestBody LikeArtical likeArtical){
        int artical_id = likeArtical.getArtical_id();
        int user_id = likeArtical.getUser_id();

        int articalLikeInsert = articalService.ArticalLikeInsert(artical_id,user_id);
        int articalLikeUpdate = articalService.ArticalLikeUpdate(artical_id,user_id);

        if(articalLikeInsert!=0&&articalLikeUpdate!=0){
            return CommonResult.success("点赞成功");
        }
        else{
            return CommonResult.failed("点赞失败");
        }
    }

    /**
     * 取消点赞文章
     * 收取数据包：用户id与文章id
     * 传回数据包：影响sql行数
     */
    @ApiOperation("取消点赞文章")
    @RequestMapping(value = "/artical/articallikeCancel",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalLikeCancel(@RequestBody LikeArtical likeArtical){
        int artical_id = likeArtical.getArtical_id();
        int user_id = likeArtical.getUser_id();

        int articalLikeCancelDelete = articalService.ArticalLikeCancelDelete(artical_id,user_id);
        int articalLikeCancelUpdate = articalService.ArticalLikeCancelUpdate(artical_id,user_id);

        if(articalLikeCancelDelete!=0&&articalLikeCancelUpdate!=0){
            return CommonResult.success("取消点赞成功");
        }
        else{
            return CommonResult.failed("取消点赞失败");
        }
    }

    /**
     * 所有文章
     * 收取数据包：
     * 传回数据包：所有文章
     */
    @ApiOperation("获取所有文章")
    @RequestMapping(value = "/artical/allartical",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allArtical(){
        List<Artical> allartical_res = articalService.AllArtical();

        if (allartical_res != null) {
            return CommonResult.success(allartical_res,"获取成功");
        }
        else{
            return CommonResult.failed("获取失败");
        }
    }

    /**
     * 所有该用户的文章
     * 收取数据包：用户id
     * 传回数据包：所有该用户文章
     */
    @ApiOperation("获取一个用户的所有文章")
    @RequestMapping(value = "/artical/allarticalinuser",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allArticalInUser(@RequestBody Artical artical){
        int user_id = artical.getUser_id();

        List<Artical> allArticalInUser_res = articalService.AllArticalInUser(user_id);

        if(allArticalInUser_res!=null){
            return CommonResult.success(allArticalInUser_res,"获取成功");
        }
        else{
            return CommonResult.failed("获取失败");
        }
    }

    /**
     * 从id获取实体
     * 收取数据包：文章id
     * 传回数据包：id对应文章
     */
    @ApiOperation("从文章id获取文章实体")
    @RequestMapping(value = "/artical/articalidtoartical",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult articalIdToArtical(@RequestBody Artical artical){
        int artical_id = artical.getArtical_id();

        Artical articalIdToArtical_res = articalService.ArticalIdToArtical(artical_id);

        if(articalIdToArtical_res!=null){
            return CommonResult.success(articalIdToArtical_res,"映射实体成功");
        }
        else{
            return CommonResult.failed("映射实体失败");
        }
    }

    /**
     * 该用户点赞的文章
     * 收取数据包：用户id
     * 传回数据包：所有该用户文章
     */
    @ApiOperation("获取一个用户点赞过的所有文章")
    @RequestMapping(value = "/artical/allarticalinlike",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allArticalInLike(@RequestBody LikeArtical likeArtical){
        int user_id = likeArtical.getUser_id();

        List<LikeArtical> allArticalInLike_res = articalService.AllArticalInLike(user_id);

        List<Artical> allArticalInLike_resArtical = new ArrayList<Artical>();
        int artical_id = 0;
        Artical artical_entity = null;

        for(int i=0;i<allArticalInLike_res.size();i++){
            artical_id = allArticalInLike_res.get(i).getArtical_id();
            artical_entity = articalService.ArticalIdToArtical(artical_id);
            allArticalInLike_resArtical.add(artical_entity);
        }

        if(allArticalInLike_resArtical!=null){
            return CommonResult.success(allArticalInLike_resArtical,"点赞文章获取成功");
        }
        else{
            return CommonResult.success("无点赞内容");
        }
    }

    /**
     * 该用户收藏的文章
     * 收取数据包：用户id
     * 传回数据包：所有该用户收藏的文章
     */
    @ApiOperation("获取一个用户点赞过的所有文章")
    @RequestMapping(value = "/artical/allarticalinsubscribe",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allArticalInSubscribe(@RequestBody SubscribeArtical subscribeArtical){
        int user_id = subscribeArtical.getUser_id();

        List<SubscribeArtical> allArticalInSubscribe_res = articalService.AllArticalInSubscribe(user_id);

        List<Artical> allArticalInSubscribe_resArtical = new ArrayList<Artical>();
        int artical_id = 0;
        Artical artical_entity = null;

        for(int i=0;i<allArticalInSubscribe_res.size();i++){
            artical_id = allArticalInSubscribe_res.get(i).getArtical_id();
            artical_entity = articalService.ArticalIdToArtical(artical_id);
            allArticalInSubscribe_resArtical.add(artical_entity);
        }

        if(allArticalInSubscribe_resArtical!=null){
            return CommonResult.success(allArticalInSubscribe_resArtical,"收藏文章获取成功");
        }
        else{
            return CommonResult.success("无收藏内容");
        }
    }

}
